﻿Imports System.Data.SqlClient
Imports System.Data
Public Class EditBookDetails
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Check if there is no Query String, if not redirects back to default page.
        If Request.Params("ID") = "" Then Response.Redirect("Default.aspx")
        SqlDataSource6.SelectParameters("Bibliophile").DefaultValue = User.Identity.Name
        If IsPostBack = False Then
            'Opening the connection
            Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("BooksReviewDBConnectionString1").ConnectionString.ToString)
            conn.Open()

            'Checking if user has rated this book
            Dim checkRatingBook As New SqlCommand("SELECT * FROM BookUserRating WHERE Bibliophile = @f1 AND Book_FK = @f2", conn)

            checkRatingBook.CommandType = CommandType.Text
            checkRatingBook.Parameters.AddWithValue("@f1", User.Identity.Name)
            checkRatingBook.Parameters.AddWithValue("@f2", Request.Params("ID"))
            'Getting records (only 1 line for selected tourist)
            Dim drRating As SqlDataReader = checkRatingBook.ExecuteReader()
            If drRating.HasRows Then
                drRating.Read()
                If Not IsDBNull(drRating("Book_FK")) Then
                    btn_Rating.Visible = False
                    btn_Change.Visible = True
                    Repeater8.Visible = True
                    Rating1.Visible = False
                End If
            End If
            drRating.Close()
            Dim subCategoryValue As Integer = 0
            Dim cmdBookDetails As New SqlCommand("SELECT * FROM Books WHERE ID = @f1", conn)
            cmdBookDetails.CommandType = CommandType.Text
            cmdBookDetails.Parameters.AddWithValue("@f1", Request.Params("ID"))
            'Add a tick if user has selected a Language.
            Dim drBookDetails As SqlDataReader = cmdBookDetails.ExecuteReader()
            If drBookDetails.HasRows Then
                drBookDetails.Read()
                If Not IsDBNull(drBookDetails("CodingLanguage_FK")) Then
                    ddl_Language.SelectedValue = drBookDetails("CodingLanguage_FK")
                End If
                If Not IsDBNull(drBookDetails("Description")) Then tb_Description.Text = drBookDetails("Description")
                If Not IsDBNull(drBookDetails("Review")) Then tb_Reviews.Text = drBookDetails("Review")
                If Not IsDBNull(drBookDetails("BookName")) Then tb_BookName.Text = drBookDetails("BookName")
            End If
            drBookDetails.Close()
            conn.Close()
        End If

    End Sub

    Protected Sub btn_Update_Click(sender As Object, e As EventArgs) Handles btn_Update.Click
        Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("BooksReviewDBConnectionString1").ConnectionString.ToString)
        Dim cmd
        If ddl_Genre.SelectedValue = 0 Then
            cmd = New SqlCommand("UPDATE Books SET BookName = @f1, Description = @f2, Review = @f3, CodingLanguage_FK = @f4 WHERE ID = @f5", conn)
        Else
            cmd = New SqlCommand("UPDATE Books SET BookName = @f1, Description = @f2, Review = @f3, CodingLanguage_FK = @f4, SubCategory_FK = @f6  WHERE ID = @f5", conn)
        End If

        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", tb_BookName.Text)
        cmd.Parameters.AddWithValue("@f2", tb_Description.Text)
        cmd.Parameters.AddWithValue("@f3", tb_Reviews.Text)
        cmd.Parameters.AddWithValue("@f4", ddl_Language.SelectedValue)
        cmd.Parameters.AddWithValue("@f5", Request.Params("ID"))
        If ddl_Genre.SelectedValue > 0 Then
            cmd.Parameters.AddWithValue("@f6", ddl_SubCategory.SelectedValue)
        End If
        conn.Open()
        cmd.ExecuteNonQuery()
        'Eliminate cmd and conn from memory
        cmd.Dispose()
        conn.Close()
        conn.Dispose()
        Response.Redirect("~/BooksPendingByBibliophile.aspx?Bibliophile=" + User.Identity.Name)
    End Sub

    Protected Function getTotalComments() As String
        Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("BooksReviewDBConnectionString1").ConnectionString.ToString)
        Dim cmd As New SqlCommand("SELECT * FROM TotalCommentsPerBook WHERE ID = @f1", conn)
        conn.Open()
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
        Dim dr As SqlDataReader = cmd.ExecuteReader()
        dr.Read()
        Dim retValue As Integer = dr("TotalComments")
        If retValue <> 1 Then
            Return dr("TotalComments") & " Comments"
        Else
            Return dr("TotalComments") & " Comment"
        End If
        conn.Close()
        conn.Dispose()
    End Function

    Protected Sub btn_Rating_Click(sender As Object, e As EventArgs) Handles btn_Rating.Click
        'If user has selected a rating
        If Rating1.CurrentRating > 0 Then
            Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("BooksReviewDBConnectionString1").ConnectionString.ToString)
            Dim delRatingCmd As New SqlCommand("DELETE FROM BookUserRating WHERE Book_FK=@f1 AND Bibliophile=@f2", conn)
            conn.Open()
            'Delete user's rating
            delRatingCmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
            delRatingCmd.Parameters.AddWithValue("@f2", User.Identity.Name)
            delRatingCmd.ExecuteNonQuery()
            'Add new rating
            Dim addRatingCmd As New SqlCommand("INSERT INTO BookUserRating (Book_FK, UserRating, Bibliophile) VALUES (@f1, @f2, @f3)", conn)

            addRatingCmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
            addRatingCmd.Parameters.AddWithValue("@f2", Rating1.CurrentRating)
            addRatingCmd.Parameters.AddWithValue("@f3", User.Identity.Name)
            addRatingCmd.ExecuteNonQuery()
            conn.Close()
            conn.Dispose()
            addRatingCmd.Dispose()
            'Update repeaters which display the average rating and current rating
            Repeater8.DataBind()

            btn_Rating.Visible = False
            btn_Change.Visible = True
            Repeater8.Visible = True
            Rating1.Visible = False
        End If
    End Sub

    Protected Sub btn_Change_Click(sender As Object, e As EventArgs) Handles btn_Change.Click
        btn_Rating.Visible = True
        btn_Change.Visible = False
        Repeater8.Visible = False
        Rating1.Visible = True
    End Sub

    Protected Function getRating(ByVal rating As Integer) As String
        Select Case (rating)
            Case Is = 1
                Return "Images/Rating/1-star-rating.png"
            Case Is = 2
                Return "Images/Rating/2-star-rating.png"
            Case Is = 3
                Return "Images/Rating/3-star-rating.png"
            Case Is = 4
                Return "Images/Rating/4-star-rating.png"
            Case Is = 5
                Return "Images/Rating/5-star-rating.png"
        End Select
        Return Nothing
    End Function


    Protected Sub btn_Delete_Click(sender As Object, e As EventArgs) Handles btn_Delete.Click
        Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("BooksReviewDBConnectionString1").ConnectionString.ToString)

        Dim cmdRating As New SqlCommand("DELETE FROM BookUserRating WHERE Book_FK = @f1", conn)
        cmdRating.CommandType = CommandType.Text
        cmdRating.Parameters.AddWithValue("@f1", Request.Params("ID"))
        conn.Open()
        cmdRating.ExecuteNonQuery()

        Dim cmd As New SqlCommand("DELETE FROM Books WHERE ID = @f1", conn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
        cmd.ExecuteNonQuery()
        conn.Close()
        Response.Redirect("~/Management/Books.aspx")

    End Sub

    
End Class